package api

import (
	"context"
	"myproject/backend/types"
	"myproject/backend/utils"
)

type BaseApi struct {
	Ctx context.Context
}

func NewBaseApi() *BaseApi {
	return &BaseApi{}
}

// GetProvince 获取省份信息
func (b *BaseApi) GetProvince() types.ListCityBaseInfoResp {
	resp := types.ListCityBaseInfoResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/baseinfo/base/address/getProvince",
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 获取城市信息
func (b *BaseApi) GetCity(provinceId int) types.ListCityBaseInfoResp {
	resp := types.ListCityBaseInfoResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/baseinfo/base/address/getCity",
		Resp:   &resp,
		QueryParams: map[string]any{
			"pid": provinceId,
		},
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 获取区县信息
func (b *BaseApi) GetArea(cityId int) types.ListCityBaseInfoResp {
	resp := types.ListCityBaseInfoResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/baseinfo/base/address/getArea",
		Resp:   &resp,
		QueryParams: map[string]any{
			"pid": cityId,
		},
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 获取街道信息
func (b *BaseApi) GetStreet(areaId int) types.ListCityBaseInfoResp {
	resp := types.ListCityBaseInfoResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/baseinfo/base/address/getStreet",
		Resp:   &resp,
		QueryParams: map[string]any{
			"pid": areaId,
		},
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}
